package com.wyebus.boss.web.configuration;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import com.smile.utils.FieldException;
import com.smile.utils.OtherException;
import com.smile.utils.param.ReturnResult;
import com.smile.utils.param.ReturnResultFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



@ControllerAdvice
public class ExceptionsHandler {

	private static Log LOG = LogFactory.getLog(ExceptionsHandler.class);

	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public ReturnResult methodArgumentNotValidException(MethodArgumentNotValidException exception) {
		BindingResult br = exception.getBindingResult();

		List<FieldError> fieldErrors = br.getFieldErrors();
		StringBuilder sb = new StringBuilder();
		for (FieldError fieldError : fieldErrors) {
			if (sb.length() > 0) {
				sb.append("，");
			}
			sb.append(fieldError.getDefaultMessage());
		}

		return ReturnResultFactory.fieldFail(sb.toString());
	}

	@ExceptionHandler(BindException.class)
	@ResponseBody
	public ReturnResult bindException(BindException exception) {
		BindingResult br = exception.getBindingResult();

		List<FieldError> fieldErrors = br.getFieldErrors();
		StringBuilder sb = new StringBuilder();
		for (FieldError fieldError : fieldErrors) {
			if (sb.length() > 0) {
				sb.append("，");
			}
			sb.append(fieldError.getDefaultMessage());
		}

		return ReturnResultFactory.fieldFail(sb.toString());
	}

	@ExceptionHandler(OtherException.class)
	@ResponseBody
	public ReturnResult otherException(OtherException exception) {
		return ReturnResultFactory.other(2001, exception.getMessage());
	}

	@ExceptionHandler(ConstraintViolationException.class)
	@ResponseBody
	public ReturnResult constraintViolationException(ConstraintViolationException exception) {
		Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();

		StringBuilder sb = new StringBuilder();
		for (ConstraintViolation<?> cv : constraintViolations) {
			if (sb.length() > 0) {
				sb.append("，");
			}
			sb.append(cv.getMessage());
		}

		return ReturnResultFactory.fieldFail(sb.toString());
	}

	@ExceptionHandler(FieldException.class)
	@ResponseBody
	public ReturnResult fieldException(FieldException exception) {
		LOG.debug("", exception);

		return ReturnResultFactory.fieldFail(exception.getMessage());
	}

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ReturnResult allExceptionHandler(Exception exception) {
		LOG.debug("", exception);

		return ReturnResultFactory.exceptionFail(exception.getMessage());
	}

}